import sqlite3
import getpass
import hashlib

# Create the connection
connection = sqlite3.connect("forum.db")
cursor = connection.cursor()

# Restore the dump
install_dump = open('install.sql', 'r')
cursor.executescript(install_dump.read())
install_dump.close()

# Add an administrator account
cursor.execute('INSERT INTO User (ID, Username, Password, Permission, JoinDate, Signature) VALUES (0, "Administrator", ?, 0, date(\'now\'),"ADMIN")', (hashlib.md5(getpass.getpass("Enter new administrator password: ")).hexdigest(),))
connection.commit()

# Populate the database if the user wants us to
if raw_input("Populate Database with Sample Data? [y/N]: ").lower() == "y":
    populate_dump = open('populate.sql', 'r')
    cursor.executescript(populate_dump.read())
    populate_dump.close()
    connection.commit()
# Otherwise, ask for forum information and create a blank froum
else:
    name=raw_input("Forum Name: ")
    desc=raw_input("Forum Description: ")
    legal=raw_input("Forum Legal info [d=default]: ")
    if legal.lower() == "d":
	    legal = "<br />Copyright 2011: Sean Fox, Mark Schultz, Jacob Snyder <br /> This software is under development. Use at your own risk."
    logo=raw_input("Path to forum logo [d=default]: ")
    if logo.lower() == "d":
	    logo = "/images/logos/logo.png"
    cursor.execute('INSERT INTO Forum (Name, Description, LegalInfo, Logo) VALUES (?, ?, ?, ?)', (name,desc,legal,logo,))
    connection.commit()
    print "you entered: "
    print name
    print desc
    print legal
    print logo
